package com.niu.wm.web.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.niu.wm.model.ProductCategory;
import com.niu.wm.core.ResponseObject;
import com.niu.wm.core.error.BizException;
import com.niu.wm.core.error.GlobalErrorCode;
import com.niu.wm.model.User;
import com.niu.wm.service.CategoryService;
import com.niu.wm.service.ProductService;

@Controller
@RequestMapping(value = "/category")
public class CategoryController extends BaseController{
	@Autowired
	private ProductService productService;
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping(value = "/listCategory.do")
    public String listProduct(ModelMap model){
		User user = this.getCurrentUser();
		
		
		return "product/listCategory";
	}
	
	/**
	 * 列出产品信息
	 * @return
	 */
	@RequestMapping(value = "/listData.do")
    @ResponseBody
    public Map<String, Object> listData(HttpServletRequest req,Pageable pager){
		User user = this.getCurrentUser();
		Map<String,Object> map = new java.util.HashMap<String, Object>();
		
		pager = this.convertPager(req, pager);
		
		List<ProductCategory> ls = categoryService.list();
		
		map.put("total", ls.size());
		map.put("rows", ls);
		
		return map;
	}
	
	/**
	 * 产品信息录入表格
	 * @return
	 */
	@RequestMapping(value = "/add.do")
    public String add(ModelMap model){
		ProductCategory category = new ProductCategory();
		model.put("category", category);
		
		return "product/category";
	}
	
	/**
	 * 产品信息编辑表格
	 * @return
	 */
	@RequestMapping(value = "/edit.do")
    public String edit(Long id, ModelMap model){
		ProductCategory category = categoryService.load(id);
		model.put("category", category);
		
		return "product/category";
	}
	
	/**
	 * 保存客户信息到数据库
	 * @return
	 */
	@RequestMapping(value = "/save.do")
    @ResponseBody
    public ResponseObject<ProductCategory> save(ProductCategory category){
		User user = this.getCurrentUser();
    	category.setCode( category.getCode().toUpperCase() );
    	if( StringUtils.isEmpty(category.getName()) ){
    		throw new BizException(GlobalErrorCode.INVALID_ARGUMENT, "类目名称不能为空");
    	}
    	if( StringUtils.isEmpty(category.getCode()) ){
    		throw new BizException(GlobalErrorCode.INVALID_ARGUMENT, "类目code不能为空");
    	}
		
		try{
			if(category.getId() == null){
				ProductCategory obj = categoryService.loadByCode(category.getCode());
				
				if(obj !=null)
					throw new BizException(GlobalErrorCode.INTERNAL_ERROR, "类目代码重复");
				
				categoryService.add(user, category);
			}else{
				categoryService.update(user, category);
			}
		}catch(Exception e){
			logger.error("",e);
			throw e;
		}
		return new ResponseObject<>(category);
	}
	
	/**
	 * 保存客户信息到数据库
	 * @return
	 */
	@RequestMapping(value = "/info.do")
    @ResponseBody
    public ResponseObject<ProductCategory> info(Long id){
		User user = this.getCurrentUser();
    	
		ProductCategory category = categoryService.load(id);
		
		return new ResponseObject<>(category);
	}
	
	/**
	 * 删除商品信息
	 * @return
	 */
	@RequestMapping(value = "/delete.do")
    @ResponseBody
    public ResponseObject<Integer> delete(Long id){
		User user = this.getCurrentUser();
		
		int count = 0;
		count = categoryService.delete(user, id);
		
		return new ResponseObject<Integer>(count);
	}
}
